var shopOrderCode = '';
var page = 1;

function doDetail() {
    $.ajax({
        url:'/crm/purchaseOrderC/detail.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:$('#idBox').html(),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                var editData = data.datas;
                $('#code').val(editData.code);
                shopOrderCode = editData.code;
                doGoodsOrder();
                $('#orderState').val(editData.orderState);
                $('#payMoney').val(editData.payMoney);
                $('#createTime').val(editData.createTime);
                $('#cancelTime').val(editData.cancelTime);
                $('#payTime').val(editData.payTime);
                $('#deliverTime').val(editData.deliverTime);
                $('#receiptTime').val(editData.receiptTime);
                $('#express').val(editData.express);
                $('#wayBillNum').val(editData.wayBillNum);
                $('#payChannel').val(editData.payChannel);
                $('#transactionNo').val(editData.transactionNo);
                $('#payType').val(editData.payType);
                $('#deliveryType').val(editData.deliveryType);
                $('#totalMoney').val(editData.payMoney);
                $('#saveMoney').val(editData.saveMoney);
                $('#shopName').val(editData.shopName);
                $('#shopPhone').val(editData.shopPhone);
                $('#shopMan').val(editData.shopMan);
                $('#shopLocation').val(editData.shopLocation);
                $('#addressDetail').val(editData.addressDetail);
                $('#city').val(editData.city);
                $('#userName').val(editData.userName);
                $('#phone').val(editData.phone);
                $('#memo').val(editData.memo);
                if(editData.orderStateValue==3){
                    $('#tongyi,#bohui').show();
                    $('#fahuo,#xiugai,#queRenDingDan').hide();
                }else if(editData.orderStateValue==4){
                    $('#fahuo').show();
                    $('#tongyi,#bohui,#xiugai,#queRenDingDan').hide();
                }else if(editData.orderStateValue==5){
                    $('#xiugai').show();
                    $('#tongyi,#bohui,#fahuo,#queRenDingDan').hide();
                }

                if(editData.orderStateValue==1&&editData.deliveryTypeValue==1){
                    $('#queRenDingDan').show();
                    $('#tongyi,#bohui,#fahuo,#xiugai').hide();
                }
            }
        }
    })
}

doDetail();



//同意发货
var handleValue =''
$('#tongyi,#bohui,#fahuo,#xiugai').click(function () {
    handleValue =$(this).attr('value');
    if(handleValue==4||handleValue==11){
        handleValue = $(this).attr('value');
        $('#handleDetailBox').show();
        $('#editWayBillNumBox,#expressTypeValueBox').hide();
    }else if(handleValue==1||handleValue==2){
        handleValue = '';
        $('#handleDetailBox').hide();
        if(handleValue==1){
            $('#editWayBillNumBox,#expressTypeValueBox').show();
        }else{
            $('#editWayBillNumBox,#expressTypeValueBox').show();
        }
    }
})
$('#submit').click(function () {
    $.ajax({
        url:'/crm/purchaseOrderC/edit.json',
        dataType:'json',
        data:{
            shopOrderId:$('#idBox').html(),
            handleValue:handleValue,
            handleDetail:$('#handleDetail').val(),
            expressTypeValue:$('#expressTypeValue').val(),
            wayBillNum:$('#editWayBillNum').val(),
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function (data) {
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('成功');
                $('#ckModal').removeClass('fade in');
                $('#ckModal').addClass('fade');
                $('#ckModal').attr('aria-hidden',true);
                $('#ckModal').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#ckModal input').val('');
                doDetail();
            }
        }
    })
})
$('#submit2').click(function () {
    $.ajax({
        url:'/crm/purchaseOrderC/confirmShopOrder.json',
        dataType:'json',
        data:{
            shopOrderId:$('#idBox').html(),

            password:$('#password').val(),

            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function (data) {
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('成功');
                $('#ckModal2').removeClass('fade in');
                $('#ckModal2').addClass('fade');
                $('#ckModal2').attr('aria-hidden',true);
                $('#ckModal2').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#ckModal2 input').val('');
                $('#queRenDingDan').hide();
                doDetail();
            }
        }
    })
})
function doGoodsOrder() {
    $.ajax({
        url:'/crm/purchaseOrderC/goodsOrderList.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNum:page,
            shopOrderCode:shopOrderCode,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#goodsOrder').empty();
                var datas = data.goodsOrderList;
                for(var i=0;i<datas.length;i++){
                    var serviceNum = '',
                        ifModal = '',
                        servicePrice = '';
                    if(datas[i].serviceNum==0){
                        serviceNum = ''
                        servicePrice = '';
                    }else{
                        serviceNum = datas[i].next;
                        servicePrice = datas[i].price;
                    }
                    if(datas[i].hasLmie){
                        ifModal = ''
                    }else{
                        ifModal = 'point-event:none'
                    }
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td data-toggle="modal" data-target="#codeModal" class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;'+ifModal+'" id="'+datas[i].id+'">'+datas[i].id+'</td></a>'+
                        '<td class=" ">'+datas[i].serviceMoney+'</td>'+
                        '<td class=" ">'+datas[i].serviceNum+'</td>'+
                        '<td class=" ">'+datas[i].ifHas+'</td>'+
                        '<td class="sorting_1">'+datas[i].goodsLmie+'</td>'+
                        '<td class="sorting_1">'+datas[i].goodsId+'</td>'+
                        '<td class="sorting_1">'+datas[i].unitPrice+'</td>'+
                        '<td class="center">'+datas[i].goodsName+'</td>'+
                        '<td class=" ">'+datas[i].num+'</td>'+
                        '<td class=" ">'+datas[i].totalMoney+'</td>'+
                        '<td class=" ">'+datas[i].saveMoney+'</td>'+
                        '<td class=" ">'+datas[i].code+'</td>'+
                        '<td class=" ">'+datas[i].orderState+'</td>'+
                        '<td class=" ">'+datas[i].payMoney+'</td>'+
                        '</tr>').appendTo($('#goodsOrder'));
                }
                //id处理
                $('.withdrawId').click(function(){
                    var id = $(this).html();
                    $.ajax({
                        url:'/crm/purchaseOrderC/getByGoodsOrderId.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                        },
                        type:'post',
                        success:function (data) {
                            if(data.code==1){
                                $('#goodsName').val(data.datas.goodsName);
                                $('#speName').val(data.datas.specificationName);
                                $('#goodsLmie').val(data.datas.goodsLmie);
                            }
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg);
                            }
                        }
                    })
                    //添加
                    $('#add').click(function () {
                        $.ajax({
                            url:'/crm/purchaseOrderC/addLmie.json',
                            dataType:'json',
                            data:{
                                token:localStorage.getItem('crmtoken'),
                                goodsOrderId:id,
                                goodsLmie:$('#goodsLmie').val(),
                            },
                            type:'post',
                            success:function (data) {
                                if(data.code==1){
                                    alert('添加成功');
                                    $('#codeModal').modal('hide');
                                }
                                if(data.code==-1){
                                    window.parent.location.href="/crm/login.jsp"
                                }
                                if(data.code==0){
                                    alert(data.msg);
                                }
                            }
                        })
                    })

                })

                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNo,
                    pageCount:data.pageNo,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

